﻿using System;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Offering
{
    /// <summary>
    /// Clase CookieManager. permite hacer cookies en el browser
    /// </summary>
    public class CookieManager
    {
        ///<summary>
        /// Metodo que permite settear una cookie en el browser
        ///</summary>
        ///<param name="key">Llave a guardar</param>
        ///<param name="val">Valor de la llave</param>
        ///<param name="expires">Tiempo de expiracion</param>
        ///<returns>Booleano indicando el estado de la creacion del a cookie</returns>
        public static bool SetCookie(string key, string val, TimeSpan? expires)
        {
            return SetCookie(key, val, expires, null, null, false);
        }

        ///<summary>
        /// Metodo que permite settear una cookie en el browser
        ///</summary>
        ///<param name="key">Llave a guardar</param>
        ///<param name="val">Valor de la llave</param>
        ///<param name="expires">Tiempo de expiracion</param>
        ///<param name="path">Direccion</param>
        ///<param name="domain">Dominio</param>
        ///<param name="secure">Estado de seguridad</param>
        ///<returns></returns>
        public static bool SetCookie(string key, string val, TimeSpan? expires, string path, string domain, bool secure)
        {

            try
            {
                if (!HtmlPage.BrowserInformation.CookiesEnabled) return false;
                var fullCookie = new StringBuilder();
                fullCookie.Append(string.Concat(key, "=", val));

                if (expires.HasValue)
                {
                    DateTime expire = DateTime.UtcNow + expires.Value; fullCookie.Append(string.Concat(";expires=", expire.ToString("R")));
                }

                if (path != null)
                {

                    fullCookie.Append(string.Concat(";path=", path));
                }

                if (domain != null)
                {

                    fullCookie.Append(string.Concat(";domain=", domain));
                }

                if (secure)
                {

                    fullCookie.Append(";secure");
                }

                HtmlPage.Document.SetProperty("cookie", fullCookie.ToString());
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        ///<summary>
        /// Metodo que permite recobrar el valor de la cookie
        ///</summary>
        ///<param name="key">llave a buscarle el valor</param>
        ///<returns>Valor de la cookie</returns>
        public string GetCookieValue(string key)
        {
            var res = "";
            try
            {
                var cookie = HtmlPage.Document.Cookies.Split('=');
                if (cookie[0].ToLower().Contains(key.ToLower()))
                    res = cookie[1];
                return res;
            }
            catch (Exception)
            {
                return res;
            }
        }

        /// <summary>
        /// Metodo que permite eliminar la cookie
        /// </summary>
        /// <returns></returns>
        public static void DeleteCookie(string key)
        {
            string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;
            DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);
            string cookie = String.Format("{0}=;expires={1}", key, expiration.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", cookie);
        }
        
    }
}
